﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Clothes.Comment
{

    /// <summary>
    /// 返回结果状态码
    /// </summary>
    public enum StatusCode
    {
        Succeed,
        Failed,
        Wrong
    }
    public class OperResult
    {
        private string Message;
        private Exception Exception;

        /// <summary>
        /// 状态码
        /// </summary>
        public StatusCode StatusCode { get; private set; }

  
        /// <summary>
        /// 返回结果的状态
        /// </summary>
        public bool Status { get; set; }

        /// <summary>
        /// 错误提示信息
        /// </summary>
        public string Massage { get; set; }

        /// <summary>
        /// 当错误时返回错误信息，返回数据为默认（无数据）
        /// </summary>
        /// <param name="mane"></param>
        /// <returns></returns>
        public static OperResult Failed(string name)
        {
            return new OperResult
            {
                Status = false,
                Massage = name
            };
        }

        /// <summary>
        /// 当执行成功时，返回数据
        /// </summary>
        /// <returns></returns>
        public static OperResult Success(string name)
        {
            return new OperResult
            {
                Status = true,
                Massage = name
            };
        }

        public static OperResult Wrong(Exception exception, string message = null)
        {
            return new OperResult()
            {
                StatusCode = StatusCode.Wrong,
                Message = message ?? exception.Message,
                Exception = exception
            };
        }

        public static OperResult Success()
        {
            return new OperResult()
            {
                StatusCode = StatusCode.Succeed,
                Message = "操作成功！"
            };
        }
    }
    public class OperResult<T>
    {
        /// <summary>
        /// 返回结果的状态
        /// </summary>
        public bool Status { get; set; }

        /// <summary>
        /// 错误提示信息
        /// </summary>
        public string Massage { get; set; }

        /// <summary>
        /// 返回的数据
        /// </summary>
        public T Data { get; set; }
        public StatusCode StatusCode { get; set; }
        public Exception Exception { get; private set; }
        public string Message { get; private set; }

        /// <summary>
        /// 读取结果
        /// </summary>
        /// <returns></returns>
        public T ReadResult()
        {
            if (Data is T)
            {
                return (T)Data;
            }
            else
            {
                return default(T);
            }
        }
        /// <summary>
        /// 当错误时返回错误信息，返回数据为默认（无数据）
        /// </summary>
        /// <param name="mane"></param>
        /// <returns></returns>
        public static OperResult<T> Failed(string mane)
        {
            return new OperResult<T>
            {
                Status = false,
                Massage = mane,
                Data = default
            };
        }
       

        /// <summary>
        /// 当执行成功时，返回数据
        /// </summary>
        /// <param name="da"></param>
        /// <returns></returns>
        public static OperResult<T> Success(T da)
        {
            return new OperResult<T>
            {
                Status = true,
                Data = da
            };
        }
        /// <summary>
        /// 操作异常
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static OperResult<T> Wrong(Exception exception, string message = null)
        {
            return new OperResult<T>()
            {
                StatusCode = StatusCode.Wrong,
                Message = message ?? exception.Message,
                Exception = exception
            };
        }

    }
}
